package rand3_number

import (
	"fmt"
)

func Rand3n() *map[string]int {
	init_array := []string{"1", "2", "3", "4"}
	res_num := make(map[string]int, 4*3*2)
	a1 := make([]string, 3)
	a2 := make([]string, 2)
	var r int = 1
	//var a1, a2 []string
	for i := 0; i < 4; i++ {
		//a1 = make([]string, 0, 3)
		copy(a1[0:i], init_array[0:i])
		copy(a1[i:], init_array[i+1:])
		//a1 = append(a1, init_array[0:i]...) //append会初始化多个底层数组，采用copy拼接，只需要3个底层数组作为数据源去构建所需要的3位数
		//a1 = append(a1, init_array[i+1:]...)
		//fmt.Println(init_array[i], a1)
		//fmt.Printf("%p\n", &a1[0])
		for j := 0; j < 3; j++ {
			//a2 = make([]string, 0, 2)
			//a2 = append(a2, a1[0:j]...)
			//a2 = append(a2, a1[j+1:]...)
			//fmt.Println(init_array[i], a1[j], a2)
			//fmt.Printf("%p\n", &a2[0])
			copy(a2[0:j], a1[0:j])
			copy(a2[j:], a1[j+1:])
			for k := 0; k < 2; k++ {
				//fmt.Printf("%p,%p\n", &a1[0], &a2[0]) //验证
				res_num[fmt.Sprintf("%s%s%s", init_array[i], a1[j], a2[k])] = r //将构建好的3位数存入hash表,以字符串形式作为key，value为数字，由于key唯一，通过观察r的数值有无重复可证明生成的3位数无重复，生成的3位数也可在包外使用
				r++
			}
		}
	}
	return &res_num
}

func Answer(resp *map[string]int) {
	fmt.Printf("能组成%d个不重复的3位数,它们分别是：\n", len(*resp))
	for i := range *resp {
		fmt.Printf("%s ", i)
	}
	fmt.Println()
}
